/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadTopicoPainel.java
 *
 * Created on 21/07/2011, 09:48:07
 */

package sad.visao;

import java.awt.Color;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import sad.util.DAOEvent;

/**
 *
 * @author UAB-2010
 */
public class CadSubTopicoDialog extends DialogAbstrato {

    /** Creates new form CadTopicoPainel */
    public CadSubTopicoDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(parent);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        saveTopico = new javax.swing.JPanel();
        txtNome = new javax.swing.JTextField();
        txtAbreviatura = new javax.swing.JTextField();
        listaTopico = new javax.swing.JComboBox();
        lblNome = new javax.swing.JLabel();
        lblAbreviatura = new javax.swing.JLabel();
        lblTopico = new javax.swing.JLabel();
        lblMessage = new javax.swing.JLabel();
        btnCadastrar = new javax.swing.JButton();
        btnExcluir = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        saveTopico.setBorder(javax.swing.BorderFactory.createTitledBorder("Salvar Sub-Tópico"));

        listaTopico.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Lista vazia>" }));

        lblNome.setText("Nome:");

        lblAbreviatura.setText("Abreviatura:");

        lblTopico.setText("Tópico:");

        javax.swing.GroupLayout saveTopicoLayout = new javax.swing.GroupLayout(saveTopico);
        saveTopico.setLayout(saveTopicoLayout);
        saveTopicoLayout.setHorizontalGroup(
            saveTopicoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(saveTopicoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(saveTopicoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblNome)
                    .addComponent(lblTopico)
                    .addGroup(saveTopicoLayout.createSequentialGroup()
                        .addComponent(lblAbreviatura)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 172, Short.MAX_VALUE))
                    .addGroup(saveTopicoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtNome, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(txtAbreviatura, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(listaTopico, javax.swing.GroupLayout.Alignment.LEADING, 0, 224, Short.MAX_VALUE))
                    .addComponent(lblMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE))
                .addContainerGap())
        );
        saveTopicoLayout.setVerticalGroup(
            saveTopicoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(saveTopicoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblNome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblAbreviatura)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtAbreviatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblTopico)
                .addGap(12, 12, 12)
                .addComponent(listaTopico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 13, Short.MAX_VALUE))
        );

        btnCadastrar.setText("Cadastrar");
        btnCadastrar.setActionCommand("Salvar Sub Topico");

        btnExcluir.setText("Excluir");
        btnExcluir.setVisible(false);

        btnCancelar.setText("Cancelar");
        btnCancelar.setActionCommand("Cancelar SubTopico");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addComponent(saveTopico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCadastrar, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
                    .addComponent(btnExcluir, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(13, 13, 13)
                        .addComponent(btnExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(saveTopico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCadastrar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnExcluir;
    private javax.swing.JLabel lblAbreviatura;
    private javax.swing.JLabel lblMessage;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblTopico;
    private javax.swing.JComboBox listaTopico;
    private javax.swing.JPanel saveTopico;
    private javax.swing.JTextField txtAbreviatura;
    private javax.swing.JTextField txtNome;
    // End of variables declaration//GEN-END:variables

   // <editor-fold defaultstate="collapsed" desc="Responde a eventos DAO">
    @Override
    public void modelDAOChange(DAOEvent evt) {
        switch(evt.getID()){
           case DAOEvent.DAO_CREATE:
                resetDialog();
                lblMessage.setForeground(Color.GREEN);
                lblMessage.setText("Sub Tópico"
                    + " cadastrado com sucesso!!!");
                break;
               
           case DAOEvent.DAO_EXISTE:
               JOptionPane.showMessageDialog(this, "O Sub Tópico "
                    + evt.getSource().toString() + " já existe!!!", null, 
                                            JOptionPane.WARNING_MESSAGE);
                resetDialog();
                break;
               
           case DAOEvent.DAO_ERROFIELD:
               JOptionPane.showMessageDialog(this, "O campo " + evt.getField()
                       + " não pode ficar em branco!!!", null,
                                    JOptionPane.WARNING_MESSAGE);
               focusField(evt.getField());
               break;
        }
    }// </editor-fold>

   // <editor-fold defaultstate="collapsed" desc="Retorna dados do objeto">
    public String getNome() {
        return this.txtNome.getText();
    }
    public String getAbreviatura() {
        return this.txtAbreviatura.getText();
    }

    public String getTopico(){
        String nome_abrev = (String) listaTopico.getSelectedItem();
        int barra = nome_abrev.lastIndexOf("/");
        String abrev = nome_abrev.substring(barra+1);
        return abrev;
    }// </editor-fold>

   //<editor-fold defaultstate="collapsed" desc="Métodos públicos comuns a todos os Dialogs com listas">
    @Override
    public void addOuvintes() {
        btnCadastrar.addActionListener(getSuporteEventos());
        btnExcluir.addActionListener(getSuporteEventos());
        btnCancelar.addActionListener(getSuporteEventos());
    }
    
    @Override
    public void close() {
        this.resetDialog();
        this.dispose();
    }

    @Override
    public void setEditableDados(Object[] d) {
        if(d!=null){
            txtNome.setText(d[0].toString());
            txtAbreviatura.setText(d[1].toString());
            txtAbreviatura.setEnabled(false);
            listaTopico.setSelectedItem(d[2].toString());
            btnExcluir.setVisible(true);
            btnCadastrar.setText("Alterar");
            btnCadastrar.setActionCommand("Alterar");
            saveTopico.setBorder(
                    javax.swing.BorderFactory.createTitledBorder("Alterar Topico"));
            btnCancelar.setText("Voltar");
        }
    }
    
    public void reload(Vector<String> s){
        listaTopico.setModel(new DefaultComboBoxModel(s));
    }// </editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Métodos privados, mas comuns a todos os Dialogs">
    @Override
    protected void resetDialog() {
        txtNome.setText(null);
        txtAbreviatura.setText(null);
        lblMessage.setText(null);
        txtNome.requestFocusInWindow();
    }
    
    @Override
    protected void focusField(String field) {
        if(field.equals("Nome")){
            txtNome.requestFocusInWindow();
        }
        if(field.equals("Abreviatura")){
            txtAbreviatura.requestFocusInWindow();
        }
        if(field.equals("Topico")){
            listaTopico.requestFocusInWindow();
        }
    }// </editor-fold>
}
